package cn.ffcs.uec.insurance.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.insurance.component.bean.UecPeriodstateListboxBean;
import cn.ffcs.uec.insurance.constant.UecBmConstants;
import cn.ffcs.uec.insurance.manager.UecBmManager;
import cn.ffcs.uec.insurance.model.UecBmclass;
import cn.ffcs.uec.insurance.model.UecPeriodstate;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class UecPeriodstateListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -5811398149406451671L;
	/**
	 *  查询出的实体对象
	 */
	private UecBmclass uecBmclass;
	/**
	 * 对应页面上的bean
	 */
	private UecPeriodstateListboxBean bean = new UecPeriodstateListboxBean();
	/**
	 * 实体
	 */
	private UecPeriodstate uecPeriodstate;
	/**
	 * 查询实体
	 */
	private UecPeriodstate qryUecPeriodstate;

	private UecBmManager uecBmManager = (UecBmManager) ApplicationContextUtil
			.getBean("uecBmManager");

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecPeriodstateListboxExt() {
		Executions.createComponents(UecBmConstants.UEC_PERIODSTATE_EXT_LISTBOX_ZUL,
				this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(UecBmConstants.ON_UEC_PERIODSTATE_QUERY, this,
				UecBmConstants.ON_UEC_PERIODSTATE_QUERY_RESPONSE);

		this.addForward(UecBmConstants.ON_CLEAN_UEC_PERIODSTATE, this,
				UecBmConstants.ON_CLEAN_UEC_PERIODSTATE_RESPONSE);
		
	}

	/**
	 * 初始化
	 */
	public void onCreate() {
		 this.setButtonValid(false, false,false,false);
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectUecPeriodstateList() throws Exception {
		if (this.bean.getUecPeriodstateListbox().getSelectedCount() > 0) {
			uecPeriodstate = (UecPeriodstate) bean.getUecPeriodstateListbox().getSelectedItem()
					.getValue();
		}
	}

	
	/**
	 * 人员基本信息功能，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecPeriodstateQueryResponse(ForwardEvent event) {
		uecBmclass = (UecBmclass) event.getOrigin().getData();
		if (null != uecBmclass) {
			if (! StrUtil.isEmpty(uecBmclass.getPkBmClass()) ) {//判断是否为集团数据
				this.setButtonValid(false, false, false, true);
			}else {
				this.setButtonValid(true, true,true, true);
			}
			qryUecPeriodstate = new UecPeriodstate();
			if (!StrUtil.isEmpty(uecBmclass.getPkBmClass())) {
				qryUecPeriodstate.setPkBmClass(uecBmclass.getPkBmClass());
			}
			qryUecPeriodstate.setUecBmclassId(uecBmclass.getUecBmclassId());
			this.onQueryUecPeriodstateQuery();
		} else {
			clearVariable();
			uecBmclass = null;
			ListboxUtils.clearListbox(this.bean.getUecPeriodstateListbox());
			this.bean.getUecPeriodstateListboxPaging().setTotalSize(1);
		}
	}
	/**
	 * 查询按钮
	 */
	public void onQueryUecPeriodstateQuery() {

		this.onQueryUecPeriodstateList();
		
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecPeriodstate() {
		ListboxUtils.clearListbox(this.bean.getUecPeriodstateListbox());
		this.bean.getUecPeriodstateListboxPaging().setTotalSize(1);
	}
	/**
	 * 清除
	 * 
	 * @param event
	 */
	public void onCleanUecPeriodstateResponse(ForwardEvent event){
		clearVariable();
		uecBmclass = null;
		ListboxUtils.clearListbox(this.bean.getUecPeriodstateListbox());
		this.bean.getUecPeriodstateListboxPaging().setTotalSize(1);
	}

	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	private void setButtonValid(boolean canAdd,boolean canEdit, boolean candel, boolean canView) {
		this.bean.getAddButton().setDisabled(!canAdd);
		this.bean.getEditButton().setDisabled(!canEdit);
		this.bean.getDelButton().setDisabled(!candel);
		this.bean.getViewButton().setDisabled(!canView);
	}


	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecPeriodstate() throws Exception {
		openUecPeriodstateEditWin("add");
		this.onQueryUecPeriodstateQuery();
	}
	/**
	 * 修改按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onEditUecPeriodstate() throws Exception {
		if (this.uecPeriodstate != null) {
			openUecPeriodstateEditWin("edit");
		} else{
			ZkUtil.showError("请选择你要修改的记录", "提示信息");
		}
		
	}
	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onViewUecPeriodstate() throws Exception {
		if (this.uecPeriodstate != null) {
			openUecPeriodstateEditWin("view");
		} else{
			ZkUtil.showError("请选择你要查看的记录", "提示信息");
		}
		
	}
	/**
	 * openUacStaffInfoWin
	 * 
	 * @param opType
	 */
	private void openUecPeriodstateEditWin(String opType) {
		if (null != uecBmclass) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", opType);
			map.put("uecBmclass", uecBmclass);
			map.put("uecPeriodstate", uecPeriodstate);
			if ("edit".equals(opType)) {
				map.put("uecPeriodstateId", uecPeriodstate.getId());
			}
			if ("add".equals(opType)) {
				map.put("uecPeriodstate", null);
			}
			Window window = (Window) Executions.createComponents(
					UecBmConstants.UEC_PERIODSTATE_EDIT_ZUL, this, map);
			try {
				window.doModal();
				window.addEventListener("onOK", new EventListener() {
					@Override
					public void onEvent(Event event) throws Exception {
						if (event.getData() != null) {
							// 新增或修改完成后，列表从数据库更新
							uecPeriodstate = (UecPeriodstate) event.getData();
							onQueryUecPeriodstateList();
						}
					}
				});
			} catch (SuspendNotAllowedException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 删除按钮
	 */
	public void onDelUecPeriodstate() {
		if (this.uecPeriodstate != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecPeriodstate.removeOnly();
						PubUtil.reDisplayListbox(bean.getUecPeriodstateListbox(),
								uecPeriodstate, "del");
						uecPeriodstate = null;
					}
				}
			});
		} else {
			ZkUtil.showError("请选择你要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页
	 */
	public void onUecPeriodstateListboxPaging() {
		this.onQueryUecPeriodstateList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecPeriodstate = null;
		qryUecPeriodstate = null;
	}

	/**
	 * 查询
	 */
	public void onQueryUecPeriodstateList() {
		if (null != uecBmclass) {
			PageInfo pageInfo = uecBmManager.queryPageInfoByUecPeriodstate(
					uecBmclass, this.bean.getUecPeriodstateListboxPaging()
							.getActivePage() + 1, this.bean
							.getUecPeriodstateListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			this.bean.getUecPeriodstateListbox().setModel(list);
			this.bean.getUecPeriodstateListboxPaging().setTotalSize(
					pageInfo.getTotalCount());
			
		}
	}
}
